/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

package com.huawei.saas.interceptor;

import com.huawei.saas.constants.Constants;
import com.huawei.saas.context.TenantContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 初始化租户标识
 *
 * @since 2022-02-14
 */
public class TenantDomainInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String tenantDomain = request.getHeader(Constants.TENANT_DOMAIN);
        TenantContext.setDomain(tenantDomain, true);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception ex) {
        TenantContext.remove();
    }
}
